package com.mapeditor.toolbar 
{
	import AS3Events.AS3EventMessage;
	import AS3Events.AS3EventRouter;
	import AS3Util.Tr;
	import com.mapeditor.graph.Container;
	import com.mapeditor.math.Vector3;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author Osvaldo Cassaniga Junior
	 */
	public class MenuShapeItem extends Sprite
	{
		public var prop:MenuShapeItemProperties;
		public var btn:SmallButton;
		private var startTime:Number;
		public var subMenuOpen:Boolean;
		private var _shapeShadowCreate:Sprite;
		
		public function MenuShapeItem(prop:MenuShapeItemProperties) 
		{
			this.subMenuOpen = false;
			
			this.prop = prop;
			this.btn = new SmallButton(this.prop.w+6,this.prop.h+6);
			this.btn.setContentImg(this.prop.btnPath,this.prop.w,this.prop.h);
			
			if (this.prop.nm != "")
				this.btn.name = this.prop.nm;
			this.addChild(this.btn);
			
			this.btn.addEventListener(MouseEvent.MOUSE_OVER, _btnOver);			
			this.btn.addEventListener(MouseEvent.MOUSE_DOWN, _btnDown);			
			
			
		}
		
		public function destructor():void {
			
		}
		
		/**
		 * Evento de mouse over sobre um item do menu
		 * Adiciona o submenu
		 * @param	e
		 */
		private function _btnOver(e:MouseEvent):void {
			btn.mouseOver();
			if (prop.subMenu != null)
			{
				if (contains(prop.subMenu))
					removeEventListener(Event.ENTER_FRAME, checkTime);//cancelar o checktime
				else
				{
					this.subMenuOpen = true;
					addChild(prop.subMenu);					
				}
			}
			this.btn.addEventListener(MouseEvent.MOUSE_OUT, _btnOut);	
		}
		
		/**
		 * Verifica se pode remover o submenu 
		 * @param	e
		 */
		private function checkTime(e:Event):void {
			var c:Number = new Date().getTime();
			if (c - startTime > 100)
			{
				if (this.mouseX < prop.subMenu.x || this.mouseX > prop.subMenu.x + prop.subMenu.width
					|| this.mouseY < prop.subMenu.y || this.mouseY > prop.subMenu.y + prop.subMenu.height)
				{
					this.subMenuOpen = false;
					removeChild(prop.subMenu);
					removeEventListener(Event.ENTER_FRAME, checkTime);
				}
			}
		}
		
		/**
		 * Evento de mouse out. Cria um timer com enter_frame para verificar apos um determinado tempo se pode remover o submenu.
		 * @param	e
		 */
		private function _btnOut(e:MouseEvent):void {
			btn.mouseOut();			
			this.btn.removeEventListener(MouseEvent.MOUSE_OUT, _btnOut);
			if (prop.subMenu != null && contains(prop.subMenu))
			{
				startTime = new Date().getTime();
				this.addEventListener(Event.ENTER_FRAME, checkTime);
			}
		}
		
		
		/**
		 * Evento de mouse down. Evento para criar a sombra do item para arrastar e soltar.
		 * @param	e
		 */
		private function _btnDown(e:MouseEvent):void {
						
			_shapeShadowCreate = new Sprite();
			var mySprite:Sprite = Sprite(e.currentTarget);
			var bData:BitmapData = new BitmapData(mySprite.width, mySprite.height, true);
			bData.draw(mySprite);
			
			var c:Container = Container.getInstance();
			
			var bmap:Bitmap = new Bitmap(bData); 
			var bMapCopy:Bitmap = new Bitmap(bmap.bitmapData.clone());
			bMapCopy.blendMode = BlendMode.MULTIPLY;
			_shapeShadowCreate.addChild(bMapCopy);
			_shapeShadowCreate.x = stage.mouseX - mySprite.width *0.5 - c.x;
			_shapeShadowCreate.y = stage.mouseY - mySprite.height *0.5 - c.y;
			_shapeShadowCreate.name = e.target.name;

			
			c.stage.addEventListener(MouseEvent.MOUSE_UP, _mouseUpBtn, false, 0, true);
			
			c.addChild(_shapeShadowCreate);

			_shapeShadowCreate.startDrag();
			
		}
		
		
		/**
		 * Evento de mouse up. Evento para criar um item.
		 * @param	e
		 */
		private function _mouseUpBtn(e:MouseEvent):void {
            _shapeShadowCreate.stopDrag();
		    var param:Object = new Object();
		    param.name  = _shapeShadowCreate.name;
		    
			
			var c:Container = Container.getInstance();
			param.refId = prop.refId;
			//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~	
		    var m:AS3EventMessage = AS3EventMessage.CreateMessage("event_new_item", "ok", param);
		    AS3EventRouter.SendEvent("class_menu",m);
		    
			
			c.stage.removeEventListener(MouseEvent.MOUSE_UP, _mouseUpBtn);
		    
			//TODO -> Remover do principal (quando tiver add no principal)
			
			c.removeChild(_shapeShadowCreate);   
		    //m = AS3EventMessage.CreateMessage(GATEEvents.EVENT_ENABLE_MULTI_SELECT_TEMP, "ok", true);
		    //AS3EventRouter.SendEvent(GATEEvents.CLASS_WEBMODELER,m);
		}//end of method
		
		/**
		 * Alinha os itens do menu
		 * @param	align
		 */
		public function alignSubMenu():void {
			if (prop.subMenu == null)			
				return;
			prop.subMenu.show();
			prop.subMenu.x = btn.x + btn.width;//posicao - a largura botao do menu para setar a direita do botao.
			prop.subMenu.y = btn.y + btn.height*0.5 - prop.subMenu.height*0.5;
				
		}
		/**
		 * Seta o id do item que o menu foi mostrado para enviar juntamente com o evento.
		 * @param	id
		 */
		public function setRefId(id:String):void {
			prop.refId = id;
			if (prop.subMenu != null)
				prop.subMenu.setRefId(id);			
		}
		
	}

}